/*=+--+=#=+--      UAS Severe Weather Simulation Softeware        --+=#=+--+=#*\
|          Copyright (C) 2013 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |
                                                                                
     This program is free software: you can redistribute it and/or modify       
     it under the terms of the GNU General Public License version 2 as          
     published by the Free Software Foundation.                                 
                                                                                
     This program is distributed in the hope that it will be useful,            
     but WITHOUT ANY WARRANTY; without even the implied warranty of             
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
     GNU General Public License for more details.                               
                                                                                
     You should have received a copy of the GNU General Public License          
     along with this program.  If not, see <http://www.gnu.org/licenses/>.      
                                                                                
            Jack Elston                                                         
|           elstonj@colorado.edu                                               |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/
//---------------------------------------------------------------------------

#ifndef __CAN_USB_H
#define __CAN_USB_H
//---------------------------------------------------------------------------
#include <stdio.h>              // printf
#include <string.h>             // strerror
#include <errno.h>              // errno
#include <unistd.h>             // getopt, sleep
#include <stdlib.h>             // atoi, atexit
#include <signal.h>             // signal
#include <err.h>                // err
#include <sched.h>              // sched_yield
#include <sys/time.h>           // gettimeofday

extern "C"
{
#include "usbcan32.h"           // Macros and definitions
}
//#include "libusbcan.h"            // API functions for USB-CAN-Module

#include "String.h"
#include "Thread.h"
#include "color.h"
//---------------------------------------------------------------------------

// ========= Index Definitions for the CAN Sentences ========================
#define PITOT   0
#define BARO    1
#define TEMP    2
#define GYRO    3
#define ACCEL   4
#define TIME    5
#define MAG     6
#define ENG     7
#define GPST    8
#define GPSP    9
#define GPSA   10
#define GPSV   11
#define SER0   12
#define SER1   13
#define SER2   14
#define SER3   15
#define SER4   16
#define SER5   17
#define SER6   18
#define SER7   19
#define CONF   20
#define SIMI   21
#define RCV    22

#define BUFFER_SIZE 1024

// ==========================================================================

typedef enum {
	UNKNOWN,
	CHECK_LIB,
	INIT_HW,
	READ_INFO,
	INIT_CAN_BUS,
	FIND_PICCOLO,
	SIM_RESET,
	INITIALIZED
} USBCANState;

class CANUSB : public pThread
{
	private:
		void initCANStruct();
		void initCAN();
		void closeCAN();
		UCANRET readCANBus();
		UCANRET writeCANBus(UCANRET idx);
		int  processCANMsg();

		void copyToCAN(UCANRET idx);
		void copyFromCAN(UCANRET idx);

		void setStatus(const char* status);
		void setError(int errsv);

		BYTE          bDeviceNr;
		tCanMsgStruct aCanMsg[23];
		tCanMsgStruct *pCanMsg;
		tUcanHandle   m_UcanHandle;

    int swPeriod;
		USBCANState state;

	public:
		CANUSB(String name);
		~CANUSB();
		bool init(ShmStruct * shmStructPtr);
		void update();
};

#endif
